//
//  SunSecondVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/12.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunSecondVC.h"
#import "SunSecondCell.h"
#import "MJRefresh.h"
#import "SunProductDetailVC.h"
#import <test/SunCycleProgress.h>
#import "SunVirVC.h"
#import "ColorButton.h"
#import "SelledViewController.h"
#undef Y
#define Y(y) AY(y)
@interface SunSecondVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tb;
    NSMutableArray *_dataArr;
    UIView *_test;
    int _page;
    Sun *_sun;
    //new
    UIView *_head;
    UILabel *_title;
    UIImageView *_stateSignal;
    UILabel *_basePercent;
    UILabel *_days;
    SunCycleProgress *_progress;
    UILabel *_startMoney;
    UILabel *_totalMoney;
}
@property (nonatomic, copy) NSString *over;
@property (nonatomic, copy) NSString *repay;

@end

@implementation SunSecondVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BACKGROUND;
    self.title = @"理财";
    [self initData];
    [self creatUI];
    [_sun showActivity:self];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_tb.mj_header beginRefreshing];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)initData{
    _dataArr = [NSMutableArray array];
    _sun = [[Sun alloc]init];
}

- (void)creatUI{
    _tb = [[UITableView alloc]initWithFrame:CGRectNull style:UITableViewStyleGrouped];
    _tb.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tb];
    [_tb lt_All:SunEdgesMake(20, 0, 0, 0) save:YES];
    _tb.delegate = self;
    _tb.dataSource = self;
    _tb.tableFooterView = [[UIView alloc]init];
    _tb.showsVerticalScrollIndicator = NO;
    _tb.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tb.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestToRefresh)];
    _tb.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestToMore)];
    //
    // 定义一个button
    UIView *cell = [[UIView alloc]init];
    [cell setFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
    [cell setBackgroundColor:[UIColor whiteColor]];
    UILabel *overLabel = [[UILabel alloc]initWithFrame:CGRectMake(X(40), 12, 200, 18)];
    overLabel.font = [UIFont systemFontOfSize:13];
    overLabel.textColor = [UIColor colorWithHexString:@"B9B9B9"];
    [cell addSubview:overLabel];
    UILabel *repayLabel = [[UILabel alloc]initWithFrame:CGRectMake(X(233), 13, 200, 18)];
    repayLabel.font = [UIFont systemFontOfSize:13];
    repayLabel.textColor = [UIColor colorWithHexString:@"B9B9B9"];
    [cell addSubview:repayLabel];

    UIImageView *line = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"second_line"]];
    [line setFrame:CGRectMake(X(189), 13, 3, 20)];
    [cell addSubview:line];
    if (self.over != NULL) {
        NSString *overString = [NSString stringWithFormat:@"已售罄产品 %@ 个",self.over];
        NSMutableAttributedString *overAtt = [[NSMutableAttributedString alloc]initWithString:overString];
        NSRange overRange = [overString rangeOfString:@"品"];
        [overAtt addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"D0021B"] range:NSMakeRange(overRange.location + 2, overString.length - 8)];
        [overLabel setAttributedText:overAtt];
    }
    if (self.repay != NULL) {
        NSString *repayString = [NSString stringWithFormat:@"已还款产品 %@ 个",self.repay];
        NSMutableAttributedString *repayAtt = [[NSMutableAttributedString alloc]initWithString:repayString];
        NSRange repayRange = [repayString rangeOfString:@"品"];
        [repayAtt addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"D0021B"] range:NSMakeRange(repayRange.location + 2, repayString.length - 8)];
        [repayLabel setAttributedText:repayAtt];
    }

    [self requestOverDate];
    
}
- (void)requestOverDate {
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_BORROWCCOUNT];
    [HttpRequest postURL:url withParam:nil success:^(id requestDic, NSString *msg) {
        [_sun hideActivity];
        self.over = [requestDic objectForKey:@"over"];
        self.repay = [requestDic objectForKey:@"repay"];

        [_tb reloadData];

    } failure:^(NSString *errorInfo, int code) {
        [_sun hideActivity];

    }];

}
#pragma mark request
- (void)requestToRefresh{
    _page = 1;
//    [self requestToVirtual];
    [self requestBorrowList];
}

- (void)requestToMore{
    _page ++;
    [_tb.mj_header endRefreshing];
    [_tb.mj_footer endRefreshing];
    [self requestBorrowList];
}

- (void)requestBorrowList{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_BORROWLIST];
    NSDictionary *param = @{@"page":[NSString stringWithFormat:@"%d",_page],@"status":@"1"};
    [HttpRequest postURL:url withParam:param success:^(id requestDic, NSString *msg) {
        [_sun hideActivity];
        [_tb.mj_header endRefreshing];
        [_tb.mj_footer endRefreshing];
        if (_page ==1) {
            [_dataArr removeAllObjects];
        }
        NSArray *arr = [requestDic objectForKey:@"list"];
        if (arr.count <= 0){
            [_tb.mj_footer endRefreshingWithNoMoreData];
        }else{
            [_tb.mj_footer resetNoMoreData];
        }
        [_dataArr addObjectsFromArray:arr];
        [_tb reloadData];
    } failure:^(NSString *errorInfo, int code) {
        [_sun hideActivity];
        [_tb.mj_header endRefreshing];
        [_tb.mj_footer endRefreshing];
//        [[Sun shareSun]showMsg:errorInfo];
    }];
    
}

#pragma mark tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArr.count + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == _dataArr.count) {
        return 45;
    }
    return 115;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == _dataArr.count) {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        [cell setBackgroundColor:[UIColor whiteColor]];
        UILabel *overLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 12, SCREENWIDTH/2-2, 18)];
        overLabel.font = [UIFont systemFontOfSize:13];
        overLabel.textAlignment = NSTextAlignmentCenter;
        overLabel.textColor = [UIColor colorWithHexString:@"B9B9B9"];
        [cell.contentView addSubview:overLabel];
        UILabel *repayLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH/2, 13, SCREENWIDTH/2-2, 18)];
        repayLabel.font = [UIFont systemFontOfSize:13];
        repayLabel.textAlignment = NSTextAlignmentCenter;
        repayLabel.textColor = [UIColor colorWithHexString:@"B9B9B9"];
        [cell.contentView addSubview:repayLabel];
        
        UIImageView *line = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"second_line"]];
        [line setFrame:CGRectMake(X(189), 13, 3, 20)];
        [cell.contentView addSubview:line];
        if (self.over != NULL) {
            NSString *overString = [NSString stringWithFormat:@"已售罄产品 %@ 个",self.over];
            NSMutableAttributedString *overAtt = [[NSMutableAttributedString alloc]initWithString:overString];
            NSRange overRange = [overString rangeOfString:@"品"];
            [overAtt addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"D0021B"] range:NSMakeRange(overRange.location + 2, overString.length - 8)];
            [overLabel setAttributedText:overAtt];
        }
        if (self.repay != NULL) {
            NSString *repayString = [NSString stringWithFormat:@"已还款产品 %@ 个",self.repay];
            NSMutableAttributedString *repayAtt = [[NSMutableAttributedString alloc]initWithString:repayString];
            NSRange repayRange = [repayString rangeOfString:@"品"];
            [repayAtt addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"D0021B"] range:NSMakeRange(repayRange.location + 2, repayString.length - 8)];
            [repayLabel setAttributedText:repayAtt];
        }

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        SunSecondCell *cell = [SunSecondCell cellWithTableView:tableView];
        NSDictionary *dic = _dataArr[indexPath.section];
        [cell setCellData:dic];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.section == _dataArr.count) {
        SelledViewController *selledVC = [[SelledViewController alloc]init];
        selledVC.over = self.over;
        selledVC.repay = self.repay;
        [self.navigationController pushViewController:selledVC animated:NO];
    }else{
        SunProductDetailVC *vc = [[SunProductDetailVC alloc]init];
        vc.borrowId = [_dataArr[indexPath.section] objectForKey:@"borrowId"];
        if ([[_dataArr[indexPath.section] objectForKey:@"desc"] length] > 0) {
            vc.title = [NSString stringWithFormat:@"%@ %@(%@)",[_dataArr[indexPath.section] objectForKey:@"name"],[_dataArr[indexPath.section] objectForKey:@"number"],[_dataArr[indexPath.section] objectForKey:@"desc"]];
        }else {
            vc.title = [NSString stringWithFormat:@"%@ %@",[_dataArr[indexPath.section] objectForKey:@"name"],[_dataArr[indexPath.section] objectForKey:@"number"]];
        }
        [self.navigationController pushViewController:vc animated:YES];
    }

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = BACKGROUND;
    return view;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
